package com.zlcdg.study.kotlin.com.zlcdgroup.delegates

import com.zlcdgroup.delegates.AppException
import java.text.DateFormat

/**
 *参数校验部分
 *@author pei
 *@date 2024/9/26 18:56
 *
 */
object ValidatorUtil {
    fun <T : Any> notBlank(value: T, errorMsg: String) {
        if (value !is String) {
            return
        }
        if (value.isEmpty()) {
            throw AppException(errorMsg)
        }
    }

    fun <T : Any> sizeRange(value: T, minLen: Int, maxLen: Int, errorMsg: String) {
        if (value !is String) {
            return
        }
        val length = value.count()
        if (length < minLen) {
            throw AppException(errorMsg)
        }
        if (length > maxLen) {
            throw AppException(errorMsg)
        }
    }

    fun <T : Any> numberRang(value: T, minValue: Number, maxValue: Number, errorMsg: String) {
        if (value !is Number) {
            return
        }
        if (value.toDouble() < minValue.toDouble()) {
            throw AppException(errorMsg)
        }
        if (value.toDouble() > maxValue.toDouble()) {
            throw AppException(errorMsg)
        }
    }

    fun <T : Any> email(value: T, errorMsg: String) {
        if (value !is String) {
            return
        }
        if (!value.contains("@")) {
            throw AppException(errorMsg)
        }
    }

    fun <T : Any> idCard(value: T, errorMsg: String) {
        if (value !is String) {
            return
        }
        if (!IDCardValidator.isValidIdCard(value)) {
            throw AppException(errorMsg)
        }
    }

    fun <T : Any> mobilePhone(value: T, errorMsg: String) {
        if (value !is String) {
            return
        }
        if (!StringValidator.isPhoneNumber(value)) {
            throw AppException(errorMsg)
        }
    }


    fun <T : Any> stringStart(value: T, startStrSet: Set<String>, errorMsg: String) {
        if (value !is String) {
            return
        }
        for (startStr in startStrSet) {
            if (value.startsWith(startStr)) {
                return
            }
        }
        throw AppException(errorMsg)
    }

    fun <T : Any> stringEnd(value: T, endStrSet: Set<String>, errorMsg: String) {
        if (value !is String) {
            return
        }
        for (startStr in endStrSet) {
            if (value.endsWith(startStr)) {
                return
            }
        }
        throw AppException(errorMsg)
    }

    fun <T : Any> stringContains(value: T, containStrSet: Set<String>, errorMsg: String) {
        if (value !is String) {
            return
        }
        for (startStr in containStrSet) {
            if (value.contains(startStr)) {
                return
            }
        }
        throw AppException(errorMsg)
    }


    fun <T : Any> numberInSet(value: T, dataSet: Set<Number>, errorMsg: String) {
        if (value !is Number) {
            return
        }
        if (!dataSet.contains(value)) {
            throw AppException(errorMsg)
        }
    }


    fun <T : Any> stringInSet(value: T, dataSet: Set<String>, errorMsg: String) {
        if (value !is String) {
            return
        }
        if (!dataSet.contains(value)) {
            throw AppException(errorMsg)
        }
    }

    fun <T : Any> date(value: T, dateFormat: DateFormat, errorMsg: String) {
        if (value !is String) {
            return
        }
        try {
            dateFormat.parse(value)
        } catch (e: Exception) {
            throw AppException(errorMsg)
        }
    }

    fun <T : Any> xss(value: T, errorMsg: String) {
        if (value !is String) {
            return
        }
        if (StringValidator.detectXSS(value)) {
            throw AppException(errorMsg)
        }
    }

}